/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 3/6/13
 * Time: 11:28 PM
 * To change this template use File | Settings | File Templates.
 */
var MyMenu = Class.create({
    initialize: function (o, parentNode, zIndex) {
        //o = {id, displayOptions, cssClassArray}

        //Mang chua cac MyButton
        this.childs = new Array();

        //Id cua menu
        this.id = o.id;

        this.divTag = new Element('div', { 'id': this.id });
        this.divTag.setStyle({
            position: 'absolute', top: o.displayOptions.top + 'px', left: o.displayOptions.left + 'px',
            width: o.displayOptions.width + 'px', height: o.displayOptions.height + 'px'
        });

        //        if(o.cssClassArray !== undefined && o.cssClassArray.length > 0){
        //            for(var i = 0; i< o.cssClassArray.length; i++){
        //                this.divTag.addClassName(o.cssClassArray[i]);
        //            }
        //        }

        this.zIndex = zIndex || 0;
        this.setZIndex(this.zIndex);

        //Neu co childs thi init het
        if(o.childs !== undefined){
            for (var i = 0; i < o.childs.length; i++) {
                var classDelegation = window[o.childs[i].type];

                var newBut = new classDelegation(o.childs[i], this.divTag, this.getZIndex());
                this.childs.push(newBut);
            }
        }

        if (parentNode !== undefined) {
            $(parentNode).insert(this.divTag);

        }
        else {
            $(document.body).insert(this.divTag);
        }
    },
    update: function () {
        this.childs.each(function (item) {
            item.update();
        });
    },
    draw: function () {

    },
    setTop: function (t) {
        $(this.divTag).setStyle({ top: t + 'px' });
    },
    getTop: function(){
        return parseInt($(this.divTag).getStyle("top"));
    },
    setLeft: function (l) {
        $(this.divTag).setStyle({ left: l + 'px'});
    },
    getLeft: function(){
        return parseInt($(this.divTag).getStyle("left"));
    },
    setWidth: function(w){
        $(this.divTag).setStyle({ width: l + 'px'});
    },
    getWidth: function(){
        return parseInt($(this.divTag).getStyle("width"));
    },
    setHeight: function(h){
        $(this.divTag).setStyle({ height: l + 'px' });
    },
    getHeight: function(){
        return parseInt($(this.divTag).getStyle("height"));
    },
    setZIndex: function(z){
        $(this.divTag).setStyle({zIndex: z});
    },
    getZIndex: function(){
        return parseInt($(this.divTag).getStyle("zIndex"));
    },
    setVisibility: function(t){
        if(t){
            $(this.divTag).setStyle({visibility: "visible"});
        }
        else{
            $(this.divTag).setStyle({visibility: "hidden"});
        }
    },
    isVisible: function(){
        if($(this.divTag).getStyle("visibility") == "visible"){
            return true;
        }
        return false;
    },
    getElementFromId: function (id) {
        var returnVal;

        for (var i = 0; i < this.childs.length; i++) {
            if(this.childs[i].getElementFromId !== undefined){
                var temp = this.childs[i].getElementFromId(id);
                if(temp !== undefined){
                    returnVal = temp;
                    break;
                }
            }
            else if (this.childs[i].id == id){
                returnVal = this.childs[i];
                break;
            }
        }

        return returnVal;
    }
});